import requests
import logging
import json

menu_detail = {
    "button": [
        {
            "type": "view",
            "name": "运维报告",
            "url": "http://mtmrpub.cxfhkj.com"
        }
    ]
}
logger = logging.getLogger()


def create_menu():
    payload = {
        "access_token": "19_MBXlqpPHdN64vIpJkBwAHpPTaFWpYxixeUru39VIeTtZDKMSvgEJ0K1__7BA-HYJTpin5rAvinaZJMe8Z6KIinXVXcrfwdw4ZdCPlVKejYx9o8rQmVQyQMjN_fi_b84HBQaH_whSCPaiOUTgRZSeAEALTM"
    }
    try:
        delete_response = requests.post("https://api.weixin.qq.com/cgi-bin/menu/delete", params=payload)
        delete_result = delete_response.json()
        if delete_result.get("errcode") != 0:
            logger.error(f"[CREATE_MENU] 删除菜单出现错误，{delete_result.get('errmsg')}")
            return
        requests.post("https://api.weixin.qq.com/cgi-bin/menu/create",
                      params=payload,
                      data=json.dumps(menu_detail, ensure_ascii=False).encode("utf-8"))
        logger.info(f"[CREATE_MENU] 创建菜单成功。")
    except requests.ConnectionError:
        logger.error(f"[CREATE_MENU] 创建菜单失败。")


if __name__ == '__main__':
    create_menu()
